package com.sharemarking.wa.platform.service;

import cn.afterturn.easypoi.cache.manager.IFileLoader;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.OrgMember;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.reportForm.CarListDto;
import com.sharemarking.wa.platform.dto.reportForm.DriverListDto;
import com.sharemarking.wa.platform.dto.reportForm.OrderListDto;
import com.sharemarking.wa.platform.mapper.MemberMapper;
import com.sharemarking.wa.platform.mapper.OrgMemberMapper;
import com.sharemarking.wa.platform.mapper.ReportFormMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class ReportFormService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrgMemberMapper orgMemberMapper;
    @Autowired
    private ReportFormMapper reportFormMapper;

    public ResponseParams<?> getDriverList() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);

        if (!role.equals(3) && !role.equals(4)){
            throw RequestException.create(ResponseParams.isDataError("无权限")) ;
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String,Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);

        params.put("memberId",memberId);
        params.put("role",role);
        //获取运输单位的机构id
        Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id",memberId)).getOrgId();
        params.put("orgId",orgId);


        List<DriverListDto> list = reportFormMapper.getDriverList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, reportFormMapper.getDriverListTotal(params), list.size() == pageSize));
    }

    public ResponseParams<?> getOrderList()  throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);

        if (!role.equals(3) && !role.equals(4)){
            throw RequestException.create(ResponseParams.isDataError("无权限")) ;
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String,Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);

        params.put("memberId",memberId);
        params.put("role",role);
        //获取运输单位的机构id
        Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id",memberId)).getOrgId();
        params.put("orgId",orgId);

        List<OrderListDto> list = reportFormMapper.getOrderList(params);


        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, reportFormMapper.getOrderListTotal(params), list.size() == pageSize));

    }

    public ResponseParams<?> getStatisticsList()  throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);

        if (!role.equals(3) && !role.equals(4)){
            throw RequestException.create(ResponseParams.isDataError("无权限")) ;
        }


        Map<String,Object> params = new HashMap<>();
        params.put("memberId",memberId);
        params.put("role",role);
        //获取运输单位的机构id
        Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id",memberId)).getOrgId();
        params.put("orgId",orgId);

        Map<String,Object> result = new HashMap<>();
        result.put("freightStatistics",reportFormMapper.getFreightStatistics(params));
        result.put("orderStatistics",reportFormMapper.getOrderStatistics(params));
        result.put("cancelOrderStatistics",reportFormMapper.getCancelOrderStatistics(params));
        result.put("endOrderStatistics",reportFormMapper.getEndOrderStatistics(params));
        return ResponseParams.ok(result);
    }

    public ResponseParams<?> getVehicleStatisticsList() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);

        if (!role.equals(3) && !role.equals(4)){
            throw RequestException.create(ResponseParams.isDataError("无权限")) ;
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String,Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);

        List<CarListDto> list = reportFormMapper.getVehicleStatisticsList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, reportFormMapper.getVehicleStatisticsListTotal(params), list.size() == pageSize));
    }
}
